<?php
class blogController extends Web_Controller_Base
{
    public function indexAction()
    {
        $this->listAction();
    }
    
    public function listAction()
    {
        $pageSize     = Web_Config_Page::blogPageSize();
        $navSize      = Web_Config_Page::blogNavSize();
        $page         = $this->getParam('page', 1);
        $pid          = $this->getParam('pid',  -1);
        $cid          = $this->getParam('cid',  -1);
        $classID      = -1;
        $className    = '';
        try {
            $limit = Web_Model_Navigate::getLimit($page, $pageSize);
            if($cid >= 0){
                $article = Web_Model_Article::getListBySubClassID($cid, $limit['start'], $limit['limit']);
                $classURL = "/cid/{$cid}";
                $classID = $cid;
                $className = Web_Model_Class::getClassNameByID($cid);
            }else if($pid >= 0){
                $article = Web_Model_Article::getListByMainClassID($pid, $limit['start'], $limit['limit']);
                $classURL = "/pid/{$pid}";
                $classID = $pid;
                $className = Web_Model_Class::getClassNameByID($pid);
            }else{
                $article = Web_Model_Article::getListAll($limit['start'], $limit['limit']);
                $classURL = '';
            }
            $class   = Web_Model_Class::getAllClass();
            $pageNav = Web_Model_Navigate::getPageListBar($article['total'], $page, $pageSize, $navSize);
        }catch (Exception $e){
            exit('系统异常,稍后重试');
        }
        if($className){
        	$this->setData('title', $className);
        } else {
        	$this->setData('title', '博客');
        }
        $this->setData('articles', $article['data']);
        $this->setData('class', $class);
        $this->setData('classID', $classID);
        $this->setData('classURL', $classURL);
        $this->setData('pageNav', $pageNav);
        $this->setView('Blog/index.tpl');
    }
    
    public function detailAction()
    {
        $articleID = $this->getParam('id', 0);
        try {
            $article = Web_Model_Article::getByID($articleID);
            $comment = Web_Model_Comment::get($articleID);
            $lastArticle = Web_Model_Article::getLastArticleByID($articleID);
            $nextArticle = Web_Model_Article::getNextArticleByID($articleID);
        }catch (Exception $e){
            exit('系统异常,稍后重试');
        }
        if(isset($article['article_title'])){
            $this->setData('title', $article['article_title']);
        }else{
            $this->setData('title', '文章不存');
        }
        $this->setData('article', $article);
        $this->setData('lastArticle', $lastArticle);
        $this->setData('nextArticle', $nextArticle);
        $this->setData('comment', $comment);
        $this->setView('Blog/detail.tpl');
    }
    
    public function downloadAction()
    {
        if(!Web_Model_Auth::hasRightSetting()){
            $this->forward('http://' . HOST);
        }
        $articleID = $this->getParam('id', 0);
        try {
            $article = Web_Model_Article::getByID($articleID);
        }catch (Exception $e){
            exit('系统异常,稍后重试');
        }
        $HTML_HEADER = <<<HEADER
<!DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Eshifang | {$article['article_title']}</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
</head>
<body>
HEADER;
        $fileName = $article['article_main_class_name'] . '_' . $article['article_sub_class_name'] . '_' . $article['article_title'] . '.html';
        $content = $HTML_HEADER . $article['article_content'] . '</head><body>';
        $fileName = str_replace('&lt;', '《', $fileName);
        $fileName = str_replace('&gt;', '》', $fileName);
        $fileName = str_replace(' ', '+', $fileName);
        Eren_DownLoad::txt(Eren_Filter::htmlspecialchars_decode($fileName), $content);
    }
    
    public function addAction()
    {
        if(!Web_Model_Auth::hasRightAdd()){
            $this->forward('http://' . HOST);
        }
        try {
            $class = Web_Model_Class::getAllClass();
        }catch (Eren_Exception $e){
            exit('系统异常,稍后重试');
        }
        $this->setData('title', '写文章');
        $this->setData('class', $class);
        $this->setView('Blog/edit.tpl');
    }
    
    public function editAction()
    {
        $id    = $this->getParam('id', 0);
        $article = Web_Model_Article::getByID($id);
        if (!$article){
            $this->forward('http://' . HOST . '/blog/add');
        }else if(!Web_Model_Auth::hasRightUpdateArticle($article['article_author_id'])){
            $this->forward('http://' . HOST);
        }
        $class = Web_Model_Class::getAllClass();
        $this->setData('title', '编辑文章');
        $this->setData('class', $class);
        $this->setData('article', $article);
        $this->setData('articleID', $id);
        $this->setView('Blog/edit.tpl');
    }
    
    public function editAjaxAction()
    {
        $title         = $this->getPost('title', '');
        $main_class_id = $this->getPost('main_class', 0);
        $sub_class_id  = $this->getPost('sub_class', 0);
        $articleID     = $this->getPost('article_id', 0);
        $article       = $this->getPost('article', '');
        $ret['errCode']=0;
        $ret['errMsg'] ='success';
        if(!$articleID){
            try {
                $id  = Web_Model_Article::insert($title, $main_class_id, $sub_class_id, $article);
                $ret['id'] = $id;
            }catch (Eren_DB_Exception $e){
                $ret['errCode'] = -10;
                $ret['errMsg']  = '系统异常,稍后重试';
            }catch (Exception $e){
                $ret['errCode'] = $e->getCode();
                $ret['errMsg']  = $e->getMessage();
            }
        }else{
            try {
                $id  = Web_Model_Article::update($articleID, $title, $main_class_id, $sub_class_id, $article);
            }catch (Eren_DB_Exception $e){
                $ret['errCode'] = -10;
                $ret['errMsg']  = '系统异常,稍后重试';
            }catch (Exception $e){
                $ret['errCode'] = $e->getCode();
                $ret['errMsg']  = $e->getMessage();
            }
        }
        echo json_encode($ret);
    }
    
    public function deleteAjaxAction()
    {
        $articleID = $this->getPost('article_id', 0);
        $ret['errCode']=0;
        $ret['errMsg'] ='success';
        try {
            $id = Web_Model_Article::delete($articleID);
        }catch (Eren_DB_Exception $e){
            $ret['errCode'] = -10;
            $ret['errMsg']  = '系统异常,稍后重试';
        }catch (Exception $e){
            $ret['errCode'] = $e->getCode();
            $ret['errMsg']  = $e->getMessage();
        }
        echo json_encode($ret);
    }
    
    public function addCommentAjaxAction()
    {
        $articleID = $this->getPost('article_id', '');
        $content = $this->getPost('content', '');
        $code = $this->getPost('code', '');
        $ret['errCode'] = 0;
        $ret['errMsg']  = 'success';
        if(Web_Model_AuthCode::veify($code)){
            try {
                $id = Web_Model_Comment::add($articleID, $content);
            }catch (Eren_DB_Exception $e){
                $ret['errCode'] = -10;
                $ret['errMsg']  = '系统异常,稍后重试';
            }catch (Exception $e){
                $ret['errCode'] = $e->getCode();
                $ret['errMsg']  = $e->getMessage();
            }
        }else{
            $ret['errCode'] = -10;
            $ret['errMsg']  = '验证码不正确';
        }
        echo json_encode($ret);
    }
    
    public function delCommentAjaxAction()
    {
        $commentID= $this->getPost('comment_id', '');
        $ret['errCode'] = 0;
        $ret['errMsg']  = 'success';
        try{
            Web_Model_Comment::del($commentID);
        }catch (Eren_DB_Exception $e){
            $ret['errCode'] = -10;
            $ret['errMsg']  = '系统异常,稍后重试';
        }catch (Exception $e){
            $ret['errCode'] = $e->getCode();
            $ret['errMsg']  = $e->getMessage();
        }
        echo json_encode($ret);
    }
}